<template>
    <div class="ele-body">
      <el-card shadow="never">
        <!-- 搜索表单 -->
        <el-form
          :model="where"
          label-width="77px"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
          <el-row :gutter="15">
          <el-col :lg="6" :md="12">
              <el-form-item label="单据编号:">
                <el-input
                  clearable
                  v-model="where.danjNo"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
            <el-form-item label="起始日期:">
                <el-date-picker v-model="where.riqiBegin" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <el-form-item label="结束日期:">
                <el-date-picker v-model="where.riqiEnd" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
            <el-col :lg="6" :md="12">
              <el-form-item label="商品编号:">
                <el-input
                  clearable
                  v-model="where.shangpNo"
                  placeholder=""/>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="15">
            <el-col :lg="6" :md="12">
              <el-form-item label="批号:">
                <el-input
                  clearable
                  v-model="where.lot"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
              <el-form-item label="单位名称:">
                <el-input
                  clearable
                  v-model="where.danwName"
                  placeholder=""/>
              </el-form-item>
            </el-col>
          <el-col :lg="6" :md="12">
              <el-form-item label="下传状态:">
                <el-select
                clearable
                v-model="where.zt"
                placeholder="请选择状态"
                class="ele-fluid">
                <el-option label="未同步" value="Y"/>
                <el-option label="已同步" value="N"/>
                <el-option label="E" value="E"/>
                <el-option label="H" value="H"/>
                <el-option label="ZT" value="ZT"/>
              </el-select>
              </el-form-item>
            </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="small"
                @click="reload">查询
              </el-button>
              <el-button @click="reset" size="small">重置</el-button>
            </div>
          </el-col>
        </el-row>
        </el-form>
        <!-- 数据表格 -->
        <ele-pro-table
          ref="table"
          :where="where"
          :datasource="url"
          :columns="columns"
          :selection.sync="selection"
          height="calc(100vh - 275px)">
          <!-- 表头工具栏 -->
          <template slot="toolbar" v-if="false">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit(null)"
              v-if="permission.includes('sys:c37infckscbillnew:add')">添加
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              @click="removeBatch"
              v-if="permission.includes('sys:c37infckscbillnew:dall')">删除
            </el-button>
          </template>
          <!-- 操作列 -->
          <!-- 操作列 -->
          <template slot="action" slot-scope="{row}">
            <el-link
            type="primary"
            :underline="false"
            icon="el-icon-upload2"
            @click="openEdit(row)"
            >NC回写
          </el-link>
            <el-link
              type="success"
              slot="reference"
              :underline="false"
              icon="el-icon-document"
              @click="openLog(row)"
              >日志
            </el-link>
            <el-link
            type="primary"
            :underline="false"
            icon="el-icon-search"
            @click="openDetail(row)"
            >单据明细
          </el-link>

          </template>
        </ele-pro-table>
      </el-card>
      <LogList
      :dataItem="current"
      :visible.sync="showLog"
      />
      <BillDetail
      :dataItem="current"
      :visible.sync="showDetail"
      />

      <!-- 编辑弹窗 -->
      <!-- <c37infckscbillnew-edit
        :data="current"
        :visible.sync="showEdit"
        @done="reload"/> -->
    </div>
  </template>

  <script>
  import { mapGetters } from "vuex";
  import LogList from '../../purchase/inbound/loglist.vue';
  import BillDetail from './billdetail.vue';
  // import C37InfCkScBillNewEdit from './c37infckscbillnew-edit';

  export default {
    name: 'ExamC37InfCkScBillNew',
    // components: {C37InfCkScBillNewEdit},
    components: {LogList,BillDetail},
    computed: {
      ...mapGetters(["permission"]),
    },
    data() {
      return {
        // 表格数据接口
        url: '/c37infckscbillnew/list',
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: "left"
          },
          {
            prop: 'danjNo',
            label: '订单编号',
            showOverflowTooltip: true,
            minWidth: 150,
            align: 'center'
          },
          {
            prop: 'chukNo',
            label: '出库单据编号',
            showOverflowTooltip: true,
            minWidth: 140,
            align: 'center'
          },
          {
            prop: 'riqiChar',
            label: '单据日期',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'danwName',
            label: '单位名称',
            showOverflowTooltip: true,
            minWidth: 200,
            align: 'center'
          },
          {
            prop: 'zt',
            label: '上传状态',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center',
            formatter: (row, column, cellValue) => {
            return this.statusFormat(row, column,cellValue);
            }
          },
          {
          prop: 'xierTime',
          label: '写入时间',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
          },
          // {
          //   prop: 'yewStaff',
          //   label: '业务员',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'tihWay',
          //   label: '提货方式',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'yewType',
          //   label: '业务类型',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'danjxcSide',
          //   label: '单据下传方',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'hanghao',
          //   label: '行号',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'shangpNo',
          //   label: '商品编号',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'jihNum',
          //   label: '开票数量',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'num',
          //   label: '实际数量',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'lot',
          //   label: '批次号',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'shengcChar',
          //   label: '生产日期',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'youxChar',
          //   label: '有效期',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'chonghFlg',
          //   label: '冲红标志',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'tuihReason',
          //   label: '退货原因',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'kub',
          //   label: '库别',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'khZzbm',
          //   label: 'NC组织编码',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'yezId',
          //   label: '九州通业主编码',
          //   showOverflowTooltip: true,
          //   minWidth: 140,
          //   align: 'center'
          // },
          {
          columnKey: 'action',
          label: '操作',
          width: 300,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
        ],
        // 表格搜索条件
        where: {riqiBegin:this.getFromDate(),riqiEnd:new Date(),yewType:'销售出库'},
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        showLog: false,
        showDetail:false,
      };
    },
    methods: {
      statusFormat(row, column,cellValue) {
        const daterc = row[column.property]
        if(daterc !=null){
            return cellValue=="N"?"已同步":"未同步";
            }
    },
      getFromDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth()
      let day = 1
      //month = month < 10 ? '0' + month : month
      //day = day < 10 ? '0' + day : day
      ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return new Date(year,month,day);
    },
      /* 刷新表格 */
      reload() {
        this.$refs.table.reload({where: this.where});
      },
      /* 重置搜索 */
      reset() {
        this.where = {riqiBegin:this.getFromDate(),riqiEnd:new Date(),yewType:'销售出库'};
        this.reload();
      },
      openLog(row){
      console.log(row);
      this.current = row;
      this.showLog = true;
    },
    openDetail(row){
      this.current = row;
      this.showDetail = true;
    },

      /* 显示编辑 */
      openEdit(row) {
        this.$confirm('确定要将选中的销售出库记录回写至NC吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/esbservice/xsck/'+ [row.chukNo]).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            //this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({lock: true});
        this.$http.delete('/c37infckscbillnew/delete', {id: row.id}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的销售出库/购进退出上传单表吗?', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.delete('/c37infckscbillnew/delete', {id: this.selection.map(d => d.id)}).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => {
        });
      },
    }
  }
  </script>

  <style scoped>
  .ele-form-search{
    margin-top: -15px ;
    margin-bottom: -5px;
  }
  .el-form-item {
    margin-bottom: -5px !important;
  }
  </style>
